package com.example.client.controller;

import com.example.client.entity.SysUser;
import com.example.client.service.SysUserService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private SysUserService userService;


    @GetMapping("/id/{id}")
    public SysUser userById(@PathVariable(value = "id") Integer id) {
        return userService.getById(id);
    }

    @PostMapping("/save")
    public void save(SysUser sysUser) {
        userService.save(sysUser);

    }


    @GetMapping("/test")
    public String test() {
        User principal = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(principal);
        return "yjb~~~~~~~~";
    }

    @PreAuthorize("hasAuthority('teacher_manager')")
    @GetMapping("/test1")
    public String test1(HttpServletRequest request) {
/*        String authorization = request.getHeader("Authorization");
        authorization = authorization.split(" ")[1];
        System.out.println(authorization);
        Jwt decode = JwtHelper.decode(authorization);
        String claims = decode.getClaims();
        System.out.println(claims);*/
        String auth = (String) request.getHeader("auth");
        System.out.println(auth);
        return "yjb~~~~~~~~";
    }

    @GetMapping("/timeOut")
    public String timeOut() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "yjb~!!!!!!!";
    }

    //@Secured({"ROLE_sale","ROLE_manager"})

    @GetMapping("/info/{username}")
    public SysUser user(@PathVariable String username) {
        return null;
    }
}
